package com.google.project.client.Sharing;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.project.client.Operator.OPTEntrypoint;
import com.google.project.client.ProjectManager.PMEntrypoint;
import com.google.project.client.Service.LoginService;
import com.google.project.client.Service.LoginServiceAsync;
import com.google.project.client.Service.ProjectStaffService;
import com.google.project.client.Service.ProjectStaffServiceAsync;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.ProjectStaffDTO;
import com.google.project.shared.ProjectTaskDTO;
import com.google.project.shared.UsersDTO;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class ProjectTaskStaff extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField(provided=true) CellTable<ProjectStaffDTO> ctbTaskStaff = new CellTable<ProjectStaffDTO>();
	@UiField Label lblProjectName;
	@UiField Label lblTaskName;
	@UiField SimplePager pager;
	@UiField Button btnBack;
	
	interface Binder extends UiBinder<Widget, ProjectTaskStaff> {
	}
	final MultiSelectionModel<ProjectStaffDTO> selectionModel = new MultiSelectionModel<ProjectStaffDTO>();
	private final ProjectStaffServiceAsync projectStaffSv = GWT.create(ProjectStaffService.class);
	private final LoginServiceAsync loginService = GWT
	.create(LoginService.class);
	ProjectTaskDTO currTask=new ProjectTaskDTO();
	ProjectDTO currProject=new ProjectDTO();
	List<ProjectStaffDTO> listStaff=new ArrayList<ProjectStaffDTO>();
	public ProjectTaskStaff() {
		initWidget(binder.createAndBindUi(this));
	}
	UsersDTO userDTO = new UsersDTO();
	public ProjectTaskStaff(ProjectTaskDTO task,ProjectDTO project) {
		this.currTask=task;
		this.currProject=project;
		getInfoUserLogin() ;
		initWidget(binder.createAndBindUi(this));
		lblProjectName.setText("Dự án: "+project.getProjectName());
		lblTaskName.setText("Công việc: "+task.getTaskName());
		initTable() ;
		bindData();
	}
	// get info user login from session
	public void getInfoUserLogin() {
		loginService.getCurrentUserDTO(new AsyncCallback<UsersDTO>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(UsersDTO result) {
				userDTO = result;
				
			}
		});
	}
	public void bindData() {
		projectStaffSv.getAllPrjStaff(new AsyncCallback<List<ProjectStaffDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<ProjectStaffDTO> result) {
				for (ProjectStaffDTO projectStaffDTO : result) {
					if(projectStaffDTO.getTaskId()!=null && projectStaffDTO.getTaskId().equals(currTask.getId())){
						listStaff.add(projectStaffDTO);
					}
				}
				LoadData(listStaff);
				
			}
		});
	
	}
	
	public void initTable() {
		
		TextColumn<ProjectStaffDTO> index = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO staff) {
				return staff.getIndex()+"";
			}
		};
		TextColumn<ProjectStaffDTO> userCode = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO staff) {
				return staff.getUserCode();
			}
		};
		TextColumn<ProjectStaffDTO> fullName = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO staff) {
				return staff.getFullName();
			}
		};
		TextColumn<ProjectStaffDTO> userName = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO staff) {
				return staff.getUserName();
			}
		};
		TextColumn<ProjectStaffDTO> specialize = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO staff) {
				return staff.getSpecializeName();
			}
		};
		TextColumn<ProjectStaffDTO> exp = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO staff) {
				int exp = staff.getExperient();
				String result = "";
				if (exp == 1) {
					result += " < 1 năm";
				} else if (exp == 2) {
					result += "1-3 năm";
				} else if (exp == 3) {
					result += "3-5 năm";
				} else if (exp == 4) {
					result += "5-10 năm";
				} else if (exp == 5) {
					result += " > 10 năm";
				}
				return result;
			}
		};
		TextColumn<ProjectStaffDTO> status = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO staff) {
				int status=staff.getStatus();
				String result = "";
				if(status==0){
					result+="Chưa tham gia";
				}else if(status==1){
					result+="Đang tham gia";
				}else if(status==2){
					result+="Đã hoàn thành";
				}
				return result;
			}
		};
		ctbTaskStaff.setStyleName("ttable");
		ctbTaskStaff.setWidth("100%", true);
		ctbTaskStaff.addColumn(index,"STT");
		ctbTaskStaff.setColumnWidth(index, 40.0, Unit.PX);
		ctbTaskStaff.addColumn(userCode,"Mã");
		ctbTaskStaff.setColumnWidth(userCode, 80.0, Unit.PX);
		ctbTaskStaff.addColumn(fullName, "Tên");
		ctbTaskStaff.setColumnWidth(fullName, 25.0, Unit.PCT);
		ctbTaskStaff.addColumn(userName, "Tài khoản");
		ctbTaskStaff.setColumnWidth(userName, 20.0, Unit.PCT);
		ctbTaskStaff.addColumn(specialize, "Chuyên môn");
		ctbTaskStaff.setColumnWidth(specialize, 20.0, Unit.PCT);
		ctbTaskStaff.addColumn(exp, "Kinh nghiệm");
		ctbTaskStaff.setColumnWidth(exp, 20.0, Unit.PCT);
		ctbTaskStaff.addColumn(status, "Tình Trạng");
		ctbTaskStaff.setColumnWidth(status, 20.0, Unit.PCT);
	
	}
public void LoadData(List<ProjectStaffDTO> listStaff) {
		
		ListDataProvider<ProjectStaffDTO> dataProvider = new ListDataProvider<ProjectStaffDTO>();
		
		List<ProjectStaffDTO> list = dataProvider.getList();
		int index=1;
		for (ProjectStaffDTO staff : listStaff) {
			staff.setIndex(index);
			list.add(staff);
			index++;
		}
		pager.setDisplay(ctbTaskStaff);
		pager.setPageSize(5);
		dataProvider.addDataDisplay(ctbTaskStaff);
	}
	
	@UiHandler("btnBack")
	void onBtnBackClick(ClickEvent event) {
		if(userDTO.getUserGroup()==2){
		PMEntrypoint.get().PMTaskDetail(currTask);
		}else if(userDTO.getUserGroup()==3){
		OPTEntrypoint.get().OPTTaskDetail(currTask);
		}
	}
}
